/**
 * Cargo.java
 */
package edu.gatech.startrader.trade;

import java.util.HashMap;
import java.util.Map;

/**
 * Cargo class
 * This class holds the information on the cargo ships and traders have
 * @author Team Bamf (Team 30)
 * @version 1.0
 */
public class Cargo extends Inventory {
	/**
	 * How many cargo holds this Cargo object can hold
	 */
	private int cargoHolds; 
	
	/**
	 * The number of each TradeGood the Cargo Object holds
	 */
	private final Map<TradeGood, Integer> items;
	
	/**
	 * Cargo Constructor
	 * This sets up the cargo with a given size
	 * @param size the size of the cargo
	 */
	public Cargo(int size) {
		cargoHolds = size;
		items = new HashMap<TradeGood, Integer>();//new int[TradeGood.values().length];
		for(int i = 0; i < TradeGood.values().length; i++) {
			items.put(TradeGood.values()[i], 0);
		}
	}
	
	/**
	 * checkRemainingCargoSpace method
	 * Gets the number of remaining cargo spaces left in the Cargo object
	 * @return the number of remaining cargo spaces left in the Cargo object
	 */
	public int checkRemainingCargoSpace() {
		int amount = cargoHolds * 5;
		for(int i = 0; i <  TradeGood.values().length; i++){
			amount -= items.get( TradeGood.values()[i]);
		}
		return amount;
	}
	
	/**
	 * getCargoHolds method
	 * This method gets the maximum amount of cargo that can be held.
	 * @return cargoHolds 
	 */
	public int getCargoHolds() {
		return cargoHolds;
	}
	
	/**
	 * setCargoHolds method
	 * This method sets a new maximum amount of cargo that can be held.
	 * @param ch the maximum amount of cargo
	 */
	public void setCargoHolds(int ch) {
		cargoHolds = ch;
	}
	
	/**
	 * getItems method
	 * This method gets the amount of cargo of the parameter type 
	 * (each index represents a different type; 
	 * each type contains its own amount)
	 * @param resource the TradeGood type of cargo
	 * @return cargo[type - 1]
	 */
	public int getItems(TradeGood resource) {
		return items.get(resource);
	}
	
	/**
	 * setCargo method
	 * This method is used to add to the current cargo by
	 *  storing an amount of an item to the corresponding 
	 *  category (type).  
	 * @param type the tradeGood to put into the cargo
	 * @param amount the number of the type of trade good
	 *  to put in storage
	 */
	public void setCargo(TradeGood type, int amount) {
		items.remove(type);
		items.put(type, new Integer(amount));
	}
	
	/**
	 * addCargo method
	 * Adds an amount of a certain TradeGood from the Cargo, if possible.
	 * @param type the TradeGood type
	 * @param amount the amount to remove
	 */
	public void addCargo(TradeGood type, int amount) {
		if(amount > checkRemainingCargoSpace()) {
			throw new IllegalArgumentException(
					"Cannot add more cargo than you can hold!");
		} else {
			setCargo(type, items.get(type) + amount);
		}
	}
	
	/**
	 * removeCargo Method
	 * Removes an amount of a certain TradeGood from the Cargo, if possible.
	 * @param type the TradeGood type
	 * @param amount the amount to remove
	 */
	public void removeCargo(TradeGood type, int amount) {
		if(items.get(type) - amount < 0) {
			throw new IllegalArgumentException("Cannot remove more cargo than you own!");
		} else {
			setCargo(type, new Integer(items.get(type) - amount));
		}
	}
}
